package regres;


/**
 * @author sanbok
 */
public class RegresLinear extends RegresTesters {
	double a, minA, maxA, b, minB, maxB;

	static private String labelName = "q=a*w+b";

	public void calculateParameters() {
		double sumX1X1 = 0, sumX2X2 = 0, sumX1X2 = 0, sumX1Y = 0, sumX2Y = 0, d;

		for (int i = 0; i < factorArray.length; i++) {
			double x1 = factorArray[i];
			double x2 = avrgArray[i];
			sumX1X1 += this.X1(x1) * this.X1(x1);
			sumX2X2 += this.X2(x1) * this.X2(x1);
			sumX1X2 += this.X1(x1) * this.X2(x1);
			sumX1Y += this.X1(x1) * x2;
			sumX2Y += this.X2(x1) * x2;
		}
		d = (sumX1X1 * sumX2X2) - (sumX1X2 * sumX1X2);
		a = ((sumX1Y * sumX2X2) - (sumX2Y * sumX1X2)) / d;
		b = ((sumX2Y * sumX1X1) - (sumX1Y * sumX1X2)) / d;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see simulationRegress.RegresTesters#f(java.lang.Float)
	 */
	public double f(double arg0) {
		return this.cutF(a * this.X1(arg0) + b * this.X2(arg0));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see simulationRegress.Tester#getLabelName()
	 */
	public String getLabelName() {
		// TODO Auto-generated method stub
		return labelName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see simulationRegress.RegresTesters#k()
	 */
	public int k() {
		return 2;
	}

	public String parametersAsString() {
		// ^ ' a=',(a roundTo:0.001)printString,'; b=',(b
		// roundTo:0.001)printString.
		// return ("a=" + Math.round(a) + "b=" + Math.round(b));
		return ("a=" + String.valueOf(a) + "b=" + String.valueOf(b));
	}

	public double X1(double f) {
		return f;
	}

	public double X2(double f) {
		return 1;
	}
}
